package org.example.utils.post.comment;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.dao.dao.post.PCommentDao;
import org.example.dao.dao.post.PLikeDao;
import org.example.dao.dao.post.PPostDao;
import org.example.dao.dao.post.PReplyDao;
import org.example.dao.dao.user.UUserDao;
import org.example.dao.dto.post.PostRedisDto;
import org.example.dao.entity.post.PComment;
import org.example.dao.entity.post.PLike;
import org.example.exception.ParamLossErrorException;
import org.example.skytool.commom.BeanUtils;
import org.example.skytool.commom.ContextUtil;
import org.example.skytool.commom.IdWorker;
import org.example.skytool.redis.RedisUtils;
import org.springframework.stereotype.Component;

import static org.example.skytool.redis.RedisConstants.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 22449
 */
@Component
public class RedisCommentService {

    @Resource
    private ContextUtil contextUtil;


    @Resource
    private IdWorker idWorker;


    @Resource
    private PLikeDao pLikeDao;
    @Resource
    private RedisUtils redisUtils;

    @Resource
    private PCommentDao pCommentDao;

    public String getCommentCacheHeader(String id) {
        String header = POST_MOUDLE + POST_COMMENT_CONTROLLER + POST_COMMENT_CACHE_KIND + id +
                POST_COMMENT_CACHE_DATA_KIND;
        return header;
    }

    public String getCommentLikeMemberHeader(String id) {
        String header = POST_MOUDLE + POST_COMMENT_CONTROLLER + POST_COMMENT_CACHE_KIND + id +
                POST_COMMENT_CACHE_MEMBER_KIND;
        return header;
    }


    public Boolean judgeIsMember(String id1, String id2) {
        if (redisUtils.sIsMember(getCommentLikeMemberHeader(id1), id2)) {
            return true;
        } else {
            return false;
        }
    }

    public void getCommentCache(PComment comment) {
        Long id = comment.getId();

        Map map = redisUtils.hashGetAll(getCommentCacheHeader(id.toString()));
        PostRedisDto postRedisDto = BeanUtils.mapToBean(PostRedisDto.class, map);
        if (postRedisDto != null && postRedisDto.getLikeNum() != null) {
            comment.setLikeNum(Integer.valueOf(postRedisDto.getLikeNum()));
        }

        pCommentDao.updateById(comment);
    }

    public void setCommentCache(PComment comment) {
        PostRedisDto postRedisDto = new PostRedisDto(comment);
        String id = comment.getId().toString();
        redisUtils.hashPut(getCommentCacheHeader(id), BeanUtils.beanToMap(postRedisDto));
        setCommentLikeMember(comment);
    }

    public void setCommentLikeMember(PComment pComment){
        String id = pComment.getId().toString();
        LambdaQueryWrapper<PLike> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(PLike::getLinkId, id);
        lambdaQueryWrapper.eq(PLike::getIsValid, 1);
        List<PLike> list = pLikeDao.selectList(lambdaQueryWrapper);
        String key = getCommentLikeMemberHeader(id);
        List<String> list1 = new ArrayList<>();
        for (PLike pLike : list) {
            list1.add(pLike.getUserId().toString());
        }
        if (list.size() == 0) {

        } else {
            redisUtils.setAdd(key, list1);
        }

    }

    public Long likeComment(String commentId) {
        String userId = contextUtil.getId().toString();
        PComment tComment = pCommentDao.selectById(commentId);
        if (tComment == null) {
            throw new ParamLossErrorException("id传递错误，传递的是:" + commentId);
        }
        Long likeNum;

        if (redisUtils.sIsMember(getCommentLikeMemberHeader(commentId), userId)) {
            redisUtils.sRemove(getCommentLikeMemberHeader(commentId), userId);
            likeNum = redisUtils.hashIncrBy(getCommentCacheHeader(commentId), POST_COMMENT_CACHE_MEMBER_LIKE_KIND, -1);
        } else {
            redisUtils.sAdd(getCommentLikeMemberHeader(commentId), userId);
            likeNum = redisUtils.hashIncrBy(getCommentCacheHeader(commentId), POST_COMMENT_CACHE_MEMBER_LIKE_KIND, 1);

            PLike pLike = new PLike();
            pLike.setId(idWorker.nextId());
            pLike.setUserId(Long.valueOf(userId));
            pLike.setLinkId(Long.valueOf(commentId));
            pLikeDao.insert(pLike);

        }
        return likeNum;
    }


}
